﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Service;
using Gizmox.WebGUI.Forms;
using System.Collections;
using System.Configuration;
using System.IO;

namespace Agresso.Interface.Layer.Web
{
    public class ExecuteInterface
    {
        public static void ExecuteCommissionInvoiceTCReletType()
        {
            CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
            commissionInvoiceService.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_COMMISSION_INVOICE_TCRELET);//.ImportDataSoftmarToMiddleLayer(DateTime.Today.AddDays(-1), SoftmarType.SOFTMAR_COMMISSION_INVOICE_TCRELET);
            commissionInvoiceService.ExecuteCommissionInvoiceTCReletType();
            commissionInvoiceService.ExecuteCommissionInvoiceTCReletTypeChargeDebit();
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("TCRelet");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        public static void ExecuteCommissionInvoiceProforma()
        {
            CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
            commissionInvoiceService.SetTestDateProforma(false, DateTime.Today, DateTime.Today, DateTime.Today);
            commissionInvoiceService.ClearAllProformaTransaction();
            commissionInvoiceService.ImportAllDataSoftmarToMiddleLayerProforma();
            //commissionInvoiceService.ExecuteInvoiceProformaChargeCredit(null);
            //commissionInvoiceService.ExecuteInvoiceProformaChargeDebit(null);
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("Proforma");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        public static void ExecuteStatement()
        {
            StatementService statementService = new StatementService();
            statementService.ImportAllDataSoftmarToMiddleLayer(0);
            statementService.ImportAllDataSoftmarToMiddleLayer(1);
            statementService.ExecuteStatementInterface();

            CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
            commissionInvoiceService.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE);// ImportDataSoftmarToMiddleLayer(DateTime.Today.AddDays(-1), SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE);
            commissionInvoiceService.ExecuteFreightHireInvoiceCargoReletChargeCreditOUT();

            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("Statement");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        public static void ExecuteCommissionInvoiceVoyageCargoReletType()
        {
            //try
            //{
            //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
            //bool lastexecute = m_lastExecuteDateVoyageCargoRelet == time;
            //if (!lastexecute)
            //{
            //    CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
            //    evt.WriteEntry(string.Format("Execute Import Commission Invoice VC Relet Type Interface To Interface Temp Table on {0}", DateTime.Now));
            //    commissionInvoiceService.ImportAllDataSoftmarToMiddleLayer();//.ImportDataSoftmarToMiddleLayer(DateTime.Today.AddDays(-1), SoftmarType.SOFTMAR_COMMISSION_INVOICE_VOYAGE_CARGO_RELET);
            //    evt.WriteEntry(string.Format("Execute Import Commission Invoice VC Relet Type Interface To Interface Temp Table on {0} Succeed", DateTime.Now));
            //    evt.WriteEntry(string.Format("Execute Commission Invoice VC Relet Type Interface on {0}", DateTime.Now));
            //    commissionInvoiceService.ExecuteCommissionInvoiceVCReletType();
            //    ConfigurationSettingService confService = new ConfigurationSettingService();
            //    ConfigurationSetting cs = confService.GetConfigurationSetting("VoyageCargoRelet");
            //    cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            //    confService.UpdateConfiguration(cs);
            //    m_lastExecuteDateVoyageCargoRelet = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
            //}
            //}
            //catch (Exception x)
            //{
            //    evt.WriteEntry(string.Format("Error while Execute  Commission Invoice VC Relet Type : {0}", x.Message));
            //}
        }
        public static void ExecuteCommissionInvoiceFreightHireType()
        {
            CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
            commissionInvoiceService.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE);// ImportDataSoftmarToMiddleLayer(DateTime.Today.AddDays(-1), SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE);
            commissionInvoiceService.ExecuteFreightHireInvoiceCargoReletChargeDebitIN();
            commissionInvoiceService.ExecuteFreightHireInvoiceCargoReletCommissionCreditIN();
            commissionInvoiceService.ExecuteFreightHireInvoiceCargoReletChargeCreditOUT();
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("FreightHireInvoice");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        public static void ExecuteVoyageMaster()
        {
            VoyageMasterService vms = new VoyageMasterService();
            vms.ImportAllDataSoftmarToMiddleLayer();// ImportDataSoftmarToMiddleLayer(DateTime.Today.AddDays(-1), SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE);
            vms.ExecuteVoyageMasterInterface();
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("VoyageMaster");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        public static void ExecuteBunkerPO()
        {
            BunkerPOService bunkerpoService = new BunkerPOService();
            bunkerpoService.ImportAllDataSoftmarToMiddleLayer();
            bunkerpoService.ExecuteBunkerProcurementPO();
            bunkerpoService.UpdateApodettextOrderID();
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("BunkerPO");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        public static void ExecuteBunkerGRN()
        {
            BunkerGRNService bunkergrnService = new BunkerGRNService();
            bunkergrnService.ImportAllDataSoftmarToMiddleLayer();
            bunkergrnService.ExecuteBunkerGRNByPO();
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("BunkerGRN");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        public static void ExecuteWinSDSPO()
        {
            WinSDSService winsdsService = new WinSDSService();
            winsdsService.ExecuteInterfaceAllPOData();
            winsdsService.UpdatePOID();
            winsdsService.UpdatePOApprovedDate();
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("WinSDSPO");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        private static void ExecuteEmailAlert()
        {
            UserService m = new UserService();
            CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
            StatementService statementService = new StatementService();
            BunkerPOService bunkerPOService = new BunkerPOService();
            BunkerGRNService bunkerGRNService = new BunkerGRNService();
            BunkerInventoryService bunkerInventoryService = new BunkerInventoryService();
            VoyageMasterService voyageService = new VoyageMasterService();
            WinSDSService winsdsService = new WinSDSService();

            int pendingSoftmarCommissionInvoice = commissionInvoiceService.GetAllPendingTCReletCommission().Count;
            int pendingSoftmarFreightHireInvoice = commissionInvoiceService.GetAllPendingFreightHireCommission().Count;
            int pendingSoftmarStatement = statementService.GetAllPendingStatement().Count;
            int pendingSoftmarBunkerPO = bunkerPOService.GetAllPendingPendingBunkerPO().Count;
            int pendingSoftmarBunkerGRN = bunkerGRNService.GetAllPendingPendingPendingPO().Count;
            int pendingSoftmarBunkerInventory = bunkerInventoryService.GetAllPendingBunkerInventory().Count;
            int pendingSoftmarVoyageMaster = voyageService.GetAllPendingVoyageMaster().Count;
            int pendingWinSDSPO = winsdsService.GetAllPendingWinSDSPO().Count;

            IList users = m.LoadAllUser();

            string file = ConfigurationManager.AppSettings["StartPath"] + "EmailBody.html";
            StreamReader rd = new StreamReader(file);
            string body = rd.ReadToEnd();
            rd.Close();
            string subject = "Agresso Interface : ";
            string softmarPending = string.Format(@"SOFTMAR :<br> 
                                        - Commission Invoice : {0} item(s)<br> 
                                        - Freight Hire Invoice : {1} item(s)<br> 
                                        - Statement Invoice : {2} item(s)<br> 
                                        - Bunker PO : {3} item(s)<br> 
                                        - Bunker GRN : {4} item(s)<br> 
                                        - Bunker Inventory : {5} item(s)<br> 
                                        - Voyage Master : {6} item(s)<br> 
                <br>", pendingSoftmarCommissionInvoice, pendingSoftmarFreightHireInvoice, pendingSoftmarStatement
                , pendingSoftmarBunkerPO, pendingSoftmarBunkerGRN, pendingSoftmarBunkerInventory, pendingSoftmarVoyageMaster);
            string winSDSPending = string.Format(@"WINSDS :<br>
                 - Purchase Order : {0} items<br>
                 <br>", pendingWinSDSPO);

            foreach (User user in users)
            {
                if (user.SendAlertToEmail)
                {
                    if (user.EmailID != "")
                    {
                        string alert = (user.SoftmarAccess ? softmarPending : "") + (user.WinSDSAccess ? winSDSPending : "");
                        EmailService.SendByLocalServer(subject + (user.SoftmarAccess ? "SOFTMAR " : "") + (user.WinSDSAccess ? "WINSDS " : "")
                            , string.Format(body, alert), user.EmailID);
                    }
                }
            }
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("EmailAlert");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        internal static void Execute(ConfigurationSetting conf)
        {
            switch (conf.setting_name)
            {
                case "TCRelet":
                    ExecuteCommissionInvoiceTCReletType();
                    break;
                case "Statement":
                    ExecuteStatement();
                    break;
                case "FreightHireInvoice":
                    ExecuteCommissionInvoiceFreightHireType();
                    break;
                case "VoyageMaster":
                    ExecuteVoyageMaster();
                    break;
                case "BunkerPO":
                    ExecuteBunkerPO();
                    break;
                case "BunkerGRN":
                    ExecuteBunkerGRN();
                    break;
                case "WinSDSPO":
                    ExecuteWinSDSPO();
                    break;
                case "BunkerInventory":
                    ExecuteBunkerInventory();
                    break;
                case "EmailAlert":
                    ExecuteEmailAlert();
                    break;
                case "CrewPayroll":
                    ExecuteCrewPayroll();
                    break;
                case "Proforma":
                    ExecuteCommissionInvoiceProforma();
                    break;
            }
        }
        private static void ExecuteBunkerInventory()
        {

            BunkerInventoryService bunkerINvService = new BunkerInventoryService();
            bunkerINvService.ImportAllDataSoftmarToMiddleLayerBODandBOR();
            bunkerINvService.ImportAllDataSoftmarToMiddleLayerTotalConsumption();
            bunkerINvService.ExecuteInterfaceIWTotalConsumption();
            bunkerINvService.ExecuteInterfaceIWBunkerOnDelivery();
            bunkerINvService.ExecuteInterfaceIRBunkerOnReDelivery();
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("BunkerInventory");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
        private static void ExecuteCrewPayroll()
        {
            CrewPayrollService cpvService = new CrewPayrollService();
            cpvService.ExecuteCrewPayrollInterface();
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("CrewPayroll");
            cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            confService.UpdateConfiguration(cs);
        }
    }
}